
************************************************************************************************
************************************************************************************************
*This .do file creates the results relating to the RD design for English (2a replicates the same .do file effectively but replaces English with math)

*Input:
*1. "student_mb_2009to2017.dta" (created by "Data_get_2009_17.do")

*Results created by .do file (in order):
*1. Column4 (4) and (2) of Table A.1
*2. Figure C.1(b)
*3. Figures A.2(a) and A.2(b) 
*4. Table 2 Columns (3) and (4)
*5. Table A.2 Columns (3) and (4)
*6. Table C.1 Columns (4), (5) and (6)
*7. Figure C.4(b)
*8. Figure C.3
************************************************************************************************
************************************************************************************************

clear all
use "/data_analysis/NC_RD_Retake/student_mb_2009to2017.dta"

*No need for post-2012 data
drop if year>2012
*Drop grade 8
drop if grade==8

*Drop the extend tests (disabled tests)
replace smathscal=. if nc_extend_math==1
replace sreadscal=. if nc_extend_read==1
drop nc_extend_math nc_extend_math_retake nc_extend_read nc_extend_read_retake
drop lag_smathscal lag_sreadscal

*Need current or subsequent scores in at least one subject to be in RD
drop if (smathscal==. | Fsmathscal==.) & (sreadscal==. | Fsreadscal==.)

*Create treatment indicator
gen treat=(running_read<=0)
qui compress
*Missing math score indicator
gen math_missing=(math==.)
foreach g of numlist 3(1)7 {
qui su math if grade==`g'
replace math=r(mean) if math_missing==1 & grade==`g'
}

*Set repeating next grade next year covariate to zero if missing
replace repeat_next_year=0 if repeat_next_year==.
replace repeating=0 if repeating==.
local demo_controls "i.sex i.lep i.swd i.ethnic i.eds i.aig_math i.aig_read i.repeating"
local test_controls "i.grade#c.math i.grade#c.read i.math_missing i.math_missing#i.grade#c.read"

*Math sample restrictions
*Need a current same subject score
drop if sreadscal==.
*Need a subsequent same subject score
drop if Fsreadscal==.

*Create dummy for reghdfe command
gen dum=1

**************************************************************
*Column (3) of Table 1: RD sample: 2008-09 to 2011-12 for grades 4-8 within 5 units of threshold
**************************************************************
*keep if running_read>=-5 & running_read<=5
*foreach var of varlist smathscal sreadscal Fsmathscal{
*su `var'
*}
*gen aig=(aig_math==1 | aig_read==1)
*foreach var of varlist ethnic eds lep swd aig repeating{
*tab `var'
*}
*drop aig
**************************************************************
**************************************************************
**************************************************************


**************************************************************
*******************Figure C.1(b)******************************
**************************************************************
*histogram running_read if running_read>=-10 & running_read<=10, discrete width(1) ylabel(0(0.03)0.09) xlabel(-10(2)10) xtitle("Distance to Retest Threshold (English)") addplot(pci 0 0 .09 0) graphregion(color(white)) bgcolor(white)
*Frandsen test:
*su running_read if running_read<10 & running_read>-10
*Choosing k: have 8 support points within 1 s.d. Corresponds to k=0.032 according's to Frandsen's rule of thumb.
*rddisttestk running_read, threshold(0) k(.032)

**************************************************************
*Figures A.2(a) and A.2(b)
**************************************************************
replace retested_r=retested_r*100
*cmogram retested_r running_read if running_read>=-5 & running_read<=5, scatter histopts(width(0.02)) cutpoint(0) lfit graphopts(xline(0) ylabel(0(20)100) ytitle("Percent Taking English Retest") xlabel(-5(1)5) xtitle("Distance to Retest Threshold (English)") graphregion(color(white)) bgcolor(white))
replace retested_r=retested_r*0.01
*reghdfe retested_r treat running_read c.running_read#i.treat if running_read>=-5 & running_read<=5, absorb(dum) cluster(schoolid mastid) 

*cmogram Fsreadscal running_read if running_read>=-5 & running_read<=5, scatter histopts(width(0.02)) cutpoint(0) lfit graphopts(xline(0) ylabel(-1(0.2)-0.2) ytitle("Standardized English Score in t+1 ({&sigma})") xlabel(-5(1)5) xtitle("Distance to Retest Threshold (English)") graphregion(color(white)) bgcolor(white))
*reghdfe Fsreadscal treat running_read c.running_read#i.treat if running_read>=-5 & running_read<=5, absorb(dum) cluster(schoolid mastid) 


**************************************************************
**************************************************************
*Table 2 Columns (3) and (4): RD regressions
**************************************************************
**************************************************************
egen gradeyear=group(grade year)
*Column (1): No covariates, column (2) covariates. t+1:
reghdfe Fsreadscal treat running_read c.running_read#i.treat if running_read>=-5 & running_read<=5, absorb(dum) cluster(schoolid mastid) 
reghdfe Fsreadscal treat running_read c.running_read#i.treat `test_controls' `demo_controls' if running_read>=-5 & running_read<=5, absorb(gradeyear) cluster(schoolid mastid) 
*t+2
reghdfe F2sreadscal treat running_read c.running_read#i.treat if running_read>=-5 & running_read<=5, absorb(dum) cluster(schoolid mastid) 
reghdfe F2sreadscal treat running_read c.running_read#i.treat `test_controls' `demo_controls' if running_read>=-5 & running_read<=5, absorb(gradeyear) cluster(schoolid mastid)
*t+3
reghdfe F3sreadscal treat running_read c.running_read#i.treat if running_read>=-5 & running_read<=5, absorb(dum) cluster(schoolid mastid) 
reghdfe F3sreadscal treat running_read c.running_read#i.treat `test_controls' `demo_controls' if running_read>=-5 & running_read<=5, absorb(gradeyear) cluster(schoolid mastid)


**************************************************************
**************************************************************
*Table A.2 Columns (3) and (4): RD regressions on Other Subject Scores
**************************************************************
**************************************************************

*Column (1): No covariates, column (2) covariates. t+1:
reghdfe Fsmathscal treat running_read c.running_read#i.treat if running_read>=-5 & running_read<=5, absorb(dum) cluster(schoolid mastid) 
reghdfe Fsmathscal treat running_read c.running_read#i.treat `test_controls' `demo_controls' if running_read>=-5 & running_read<=5, absorb(gradeyear) cluster(schoolid mastid) 
*t+2
reghdfe F2smathscal treat running_read c.running_read#i.treat if running_read>=-5 & running_read<=5, absorb(dum) cluster(schoolid mastid) 
reghdfe F2smathscal treat running_read c.running_read#i.treat `test_controls' `demo_controls' if running_read>=-5 & running_read<=5, absorb(gradeyear) cluster(schoolid mastid)
*t+3
reghdfe F3smathscal treat running_read c.running_read#i.treat if running_read>=-5 & running_read<=5, absorb(dum) cluster(schoolid mastid) 
reghdfe F3smathscal treat running_read c.running_read#i.treat `test_controls' `demo_controls' if running_read>=-5 & running_read<=5, absorb(gradeyear) cluster(schoolid mastid)
**************************************************************
**************************************************************
**************************************************************

**************************************************************
*Appendix Table C.1: Functional Form
**************************************************************
*Quadratic
gen inter=running_read*treat
gen running2=running_read*running_read
gen inter2=running_read*running_read*treat
*Column (5)
*t+1
reghdfe Fsreadscal treat running_read inter running2 inter2 `test_controls' `demo_controls' if running_read>=-5 & running_read<=5, absorb(gradeyear) cluster(schoolid mastid) 
*t+2
reghdfe F2sreadscal treat running_read inter running2 inter2 `test_controls' `demo_controls' if running_read>=-5 & running_read<=5, absorb(gradeyear) cluster(schoolid mastid)
*t+3
reghdfe F3sreadscal treat running_read inter running2 inter2 `test_controls' `demo_controls' if running_read>=-5 & running_read<=5, absorb(gradeyear) cluster(schoolid mastid)
*Other subject
reghdfe Fsmathscal treat running_read inter running2 inter2 `test_controls' `demo_controls' if running_read>=-5 & running_read<=5, absorb(gradeyear) cluster(schoolid mastid) 
*t+2
reghdfe F2smathscal treat running_read inter running2 inter2 `test_controls' `demo_controls' if running_read>=-5 & running_read<=5, absorb(gradeyear) cluster(schoolid mastid)
*t+3
reghdfe F3smathscal treat running_read inter running2 inter2 `test_controls' `demo_controls' if running_read>=-5 & running_read<=5, absorb(gradeyear) cluster(schoolid mastid)

*Triangular
gen weight=max(0,5-abs(running_read))
*Column (6)
*t+1
reghdfe Fsreadscal treat running_read inter `test_controls' `demo_controls' [aw=weight] if running_read>=-5 & running_read<=5, absorb(gradeyear) cluster(schoolid mastid) 
*t+2
reghdfe F2sreadscal treat running_read inter `test_controls' `demo_controls' [aw=weight] if running_read>=-5 & running_read<=5, absorb(gradeyear) cluster(schoolid mastid)
*t+3
reghdfe F3sreadscal treat running_read inter `test_controls' `demo_controls' [aw=weight] if running_read>=-5 & running_read<=5, absorb(gradeyear) cluster(schoolid mastid)
*Other subject
reghdfe Fsmathscal treat running_read inter `test_controls' `demo_controls' [aw=weight] if running_read>=-5 & running_read<=5, absorb(gradeyear) cluster(schoolid mastid)
*t+2
reghdfe F2smathscal treat running_read inter `test_controls' `demo_controls' [aw=weight] if running_read>=-5 & running_read<=5, absorb(gradeyear) cluster(schoolid mastid)
*t+3
reghdfe F3smathscal treat running_read inter `test_controls' `demo_controls' [aw=weight] if running_read>=-5 & running_read<=5, absorb(gradeyear) cluster(schoolid mastid)

**************************************************************
*Appendix Figure C.4(a): Bandwidth Robustness
**************************************************************
mat A=[.,.,.]
foreach b of numlist 2(1)10{
display "Bandwidth is `b'"
*Main RD Regression*
qui reghdfe Fsreadscal treat running_read c.running_read#i.treat `test_controls' `demo_controls' if running_read>=-`b' & running_read<=`b', absorb(gradeyear) cluster(schoolid mastid) 
mat C=e(V)
mat D=e(b)
mat A=A\[D[1,1], C[1,1], `b']
}
svmat A
ren A1 est
ren A2 var
ren A3 bandwidth
gen lcl=est - 1.96*((var)^0.5)
gen ucl=est + 1.96*((var)^0.5)
twoway (connected est bandwidth) (rcap lcl ucl bandwidth), xtitle("Bandwidth") xlabel(2(1)10) ylabel(0(0.01)0.05) ytitle("Estimated Coefficient (English Scores ({&sigma}))") yline(0) xline(5) graphregion(color(white)) bgcolor(white)



**************************************************************
*Appendix: RD Covariates
**************************************************************
gen asian=(ethnic==1)
gen black=(ethnic==2)
gen hisp=(ethnic==3)
gen white=(ethnic==6)
gen aig=(aig_math==1 | aig_read==1)

foreach var of varlist white black hisp asian eds lep swd repeating {
replace `var'=`var'*100
}
**************************************************************
*Figure C.3
**************************************************************
cmogram smathscal running_read if running_read>=-5 & running_read<=5, scatter histopts(width(0.02)) cutpoint(0) lfit graphopts(xline(0) ylabel(-.7(.2).1) ytitle("Standardized Mathematics Score on Initial Test ({&sigma})") xlabel(-5(1)5) xtitle("Distance to Retest Threshold (English)") graphregion(color(white)) bgcolor(white))
cmogram white running_read if running_read>=-5 & running_read<=5, scatter histopts(width(0.02)) cutpoint(0) lfit graphopts(xline(0) ylabel(35(5)60) ytitle("Percent White") xlabel(-5(1)5) xtitle("Distance to Retest Threshold (English)") graphregion(color(white)) bgcolor(white))
cmogram black running_read if running_read>=-5 & running_read<=5, scatter histopts(width(0.02)) cutpoint(0) lfit graphopts(xline(0) ylabel(20(5)40) ytitle("Percent Black") xlabel(-5(1)5) xtitle("Distance to Retest Threshold (English)") graphregion(color(white)) bgcolor(white))
cmogram hisp running_read if running_read>=-5 & running_read<=5, scatter histopts(width(0.02)) cutpoint(0) lfit graphopts(xline(0) ylabel(10(2)18) ytitle("Percent Hispanic") xlabel(-5(1)5) xtitle("Distance to Retest Threshold (English)") graphregion(color(white)) bgcolor(white))
cmogram asian running_read if running_read>=-5 & running_read<=5, scatter histopts(width(0.02)) cutpoint(0) lfit graphopts(xline(0) ylabel(1.5(.25)2.5) ytitle("Percent Asian") xlabel(-5(1)5) xtitle("Distance to Retest Threshold (English)") graphregion(color(white)) bgcolor(white))
cmogram eds running_read if running_read>=-5 & running_read<=5, scatter histopts(width(0.02)) cutpoint(0) lfit graphopts(xline(0) ylabel(50(5)70) ytitle("Percent Economically Disadvantaged") xlabel(-5(1)5) xtitle("Distance to Retest Threshold (English)") graphregion(color(white)) bgcolor(white))
cmogram lep running_read if running_read>=-5 & running_read<=5, scatter histopts(width(0.02)) cutpoint(0) lfit graphopts(xline(0) ylabel(4(2)12) ytitle("Percent Limited English Proficient") xlabel(-5(1)5) xtitle("Distance to Retest Threshold (English)") graphregion(color(white)) bgcolor(white))
cmogram swd running_read if running_read>=-5 & running_read<=5, scatter histopts(width(0.02)) cutpoint(0) lfit graphopts(xline(0) ylabel(6(2)16) ytitle("Percent with a Disability") xlabel(-5(1)5) xtitle("Distance to Retest Threshold (English)") graphregion(color(white)) bgcolor(white))
cmogram repeating running_read if running_read>=-5 & running_read<=5, scatter histopts(width(0.02)) cutpoint(0) lfit graphopts(xline(0) ylabel(1(0.5)2.5) ytitle("Percent Repeating Current Grade") xlabel(-5(1)5) xtitle("Distance to Retest Threshold (English)") graphregion(color(white)) bgcolor(white))
**************************************************************
*Table version (not in paper)
**************************************************************
foreach var of varlist smathscal white black hisp asian eds lep swd repeating {
reghdfe `var' treat running_read c.running_read#i.treat if running_read>=-5 & running_read<=5, absorb(dum) cluster(schoolid mastid) 
}






